﻿using AutoMapper;
using Modules.Person;
using PmSoft;
using static Modules.Person.PersonConstants;

namespace ManagementApi.Models.Person
{
    public class PersonBankDto
    {
        /// <summary>
        /// 主键 
        ///</summary>
        public int Id { get; set; }
        /// <summary>
        /// 人员身份证
        ///</summary>
        public string IdCard { get; set; }

        public 工资卡类型 Type { get; set; }
        /// <summary>
        /// 发放工资银行卡号 
        ///</summary>
        public string CardNo { get; set; }
        /// <summary>
        /// 发放工资银行名称 
        ///</summary>
        public string BankName { get; set; }
        /// <summary>
        /// 发放工资卡银行联号 
        ///</summary>
        public string BankLinkNo { get; set; }
        /// <summary>
        /// 发放工资卡银行 参考银行代码字典表 
        ///</summary>
        public string BankCode { get; set; }
        /// <summary>
        /// 银行地址
        ///</summary>
        public string BankAddress { get; set; }
        /// <summary>
        /// 创建时间 
        ///</summary>
        public DateTime? CreateTime { get; set; }
        /// <summary>
        /// 是否默认银行卡
        /// </summary>
        public bool IsDefault { get; set; }

        #region 扩展字段
        public string PersonName { get; set; }
        public string TypeName { get; set; }
        #endregion
    }
    public static partial class PersonBankExceptions
    {

        public static PersonBankDto AsPersonBankDto(this PersonBank entity)
        {
            var personService = ServiceAccessor.GetService<PersonService>();
            PersonInfo? personInfo = personService.GetPersonByCardNo(entity.IdCard);
            PersonBankDto dto = new PersonBankDto();
            if (personInfo != null)
            {
                var personBankService = ServiceAccessor.GetService<PersonBankService>();
               // PersonBank? bank = personBankService.GetDefaultPersonBankByIdCard(personInfo.CardNumber);         
                var mapper = ServiceAccessor.GetService<IMapper>();
                mapper.Map(entity, dto);
                dto.TypeName = entity?.Type.GetDescription()??"";
                dto.PersonName = personInfo.Name ?? string.Empty;
                dto.IdCard = personInfo.CardNumber;
            }
            return dto;
        }
    }
}
